<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 阿里巴巴找黄金宝箱(V)（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>一贫如洗的樵夫阿里巴巴在去砍柴的路上&#xff0c;无意中发现了强盗集团的藏宝地&#xff0c;藏宝地有编号从0~N的箱子&#xff0c;每个箱子上面贴有一个数字。</p> 
<p>阿里巴巴念出一个咒语数字k(k&lt;N)&#xff0c;找出连续k个宝箱数字和的最大值&#xff0c;并输出该最大值。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行输入一个数字字串&#xff0c;数字之间使用逗号分隔&#xff0c;例如&#xff1a;2,10,-3,-8,40,5</p> 
<ul><li>1 ≤ 字串中数字的个数 ≤ 100000 </li><li>-10000 ≤ 每个数字 ≤ 10000</li></ul> 
<p>第二行输入咒语数字&#xff0c;例如&#xff1a;4&#xff0c;咒语数字大小小于宝箱的个数</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>连续k个宝箱数字和的最大值&#xff0c;例如&#xff1a;39</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2,10,-3,-8,40,5<br /> 4</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">39</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">8<br /> 1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">8</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题可以使用滑动窗口解决。</p> 
<p></p> 
<p>题目说&#xff1a;</p> 
<blockquote> 
 <p>找出<span style="color:#fe2c24;">连续k个</span>宝箱数字和的最大值</p> 
</blockquote> 
<p>其中连续k个刚好可以用一个固定长度为k的滑动窗口去限定。</p> 
<p>我们只需要花费O(k)的时间求解出初始滑动窗口的数字和&#xff0c;之后每右移一个后的新状态滑窗的数字和都可以依赖于前一个滑窗的数字和求得&#xff1a;</p> 
<p><img alt="" height="279" src="https://img-blog.csdnimg.cn/2d5a65a1dc9d4a5481d19e7e8bce5986.png" width="414" /></p> 
<p>如上图所示&#xff0c;除了初始滑窗&#xff0c;所有的滑窗的数字和cur &#61; 之前滑窗的数字和last - 红色失去的数字 &#43; 绿色新增的数字</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int[] arr &#61; Arrays.stream(sc.nextLine().split(&#34;,&#34;)).mapToInt(Integer::parseInt).toArray();
    int k &#61; Integer.parseInt(sc.nextLine());

    System.out.println(getResult(arr, k));
  }

  public static int getResult(int[] arr, int k) {
    int window_sum &#61; 0;

    for (int i &#61; 0; i &lt; k; i&#43;&#43;) {
      window_sum &#43;&#61; arr[i];
    }

    int ans &#61; window_sum;

    for (int i &#61; 1; i &lt;&#61; arr.length - k; i&#43;&#43;) {
      window_sum -&#61; arr[i - 1];
      window_sum &#43;&#61; arr[i &#43; k - 1];
      ans &#61; Math.max(ans, window_sum);
    }

    return ans;
  }
}
</code></pre> 
<h4>JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61; 2) {
    const arr &#61; lines[0].split(&#34;,&#34;).map(Number);
    const k &#61; lines[1] - 0;
    console.log(getResult(arr, k));
    lines.length &#61; 0;
  }
});

function getResult(arr, k) {
  let window_sum &#61; 0;

  for (let i &#61; 0; i &lt; k; i&#43;&#43;) {
    window_sum &#43;&#61; arr[i];
  }

  let ans &#61; window_sum;

  for (let i &#61; 1; i &lt;&#61; arr.length - k; i&#43;&#43;) {
    window_sum -&#61; arr[i - 1];
    window_sum &#43;&#61; arr[i &#43; k - 1];
    ans &#61; Math.max(ans, window_sum);
  }

  return ans;
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; list(map(int, input().split(&#34;,&#34;)))
k &#61; int(input())


# 算法入口
def getResult():
    window_sum &#61; 0

    for i in range(k):
        window_sum &#43;&#61; arr[i]

    ans &#61; window_sum

    for i in range(1, len(arr) - k &#43; 1):
        window_sum -&#61; arr[i - 1]
        window_sum &#43;&#61; arr[i &#43; k - 1]
        ans &#61; max(ans, window_sum)

    return ans


# 算法调用
print(getResult())</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 100000
#define MAX(a,b) a &gt; b ? a : b

int getResult(int* arr, int arr_size, int k);

int main()
{
	int arr[MAX_SIZE];
	
	int arr_size &#61; 0;
	while(scanf(&#34;%d&#34;, &amp;arr[arr_size&#43;&#43;])) {
		if(getchar() !&#61; &#39;,&#39;) break;
	}
	
	int k;
	scanf(&#34;%d&#34;, &amp;k);
	
	printf(&#34;%d\n&#34;, getResult(arr, arr_size, k));
	
	return 0;
}

int getResult(int* arr, int arr_size, int k)
{
	int window_sum &#61; 0;
	
	for(int i&#61;0; i&lt;k; i&#43;&#43;) {
		window_sum &#43;&#61; arr[i];
	}
	
	int ans &#61; window_sum;
	
	for(int i&#61;1; i&lt;&#61;arr_size - k; i&#43;&#43;) {
		window_sum -&#61; arr[i-1];
		window_sum &#43;&#61; arr[i&#43;k-1];
		ans &#61; MAX(ans, window_sum);
	}
	
	return ans;
}</code></pre> 
<p> </p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>